﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Tasks;
using MobileSales.Website.TaskService;
using MobileSales.Website.Infrastructure;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class TasksController : ControllerBase
    {
        #region Details
        //
        // GET: /Tasks/Details/5

        public ActionResult Details(int id, int employeeId)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                Task task = client.GetTask(id);
                client.Close();
                ViewData["employeeId"] = employeeId;
                return View(task);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.TaskNotFound, MessageType.Error);
                return RedirectToAction("Index", "Schedules", new { employeeId = employeeId });
            }
        } 
        #endregion

        #region Create
        //
        // GET: /Tasks/Create

        public ActionResult Create(int? employeeId)
        {
            ViewData["employeeId"] = employeeId;
            return View();
        }

        //
        // POST: /Tasks/Create

        [HttpPost]
        public ActionResult Create(int taskTypeId, int taskStatusId, int employeeId, Task task)
        {
            try
            {
                task.TaskStatus = new TaskStatus { Id = taskStatusId };
                task.TaskType = new TaskType { Id = taskTypeId };
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreTask(task, employeeId);
                client.Close();
                this.SetMessage(InfoStrings.TaskAdded, MessageType.Info);
                return RedirectToAction("Details", "Schedules", new { employeeId = employeeId, date = task.StartTime.ToShortDateString() });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskAdding, MessageType.Error);

            }
            return View(task);
        } 
        #endregion

        #region Edit
        //
        // GET: /Tasks/Edit/5

        public ActionResult Edit(int id, int employeeId)
        {
            ViewData["employeeId"] = employeeId;
            return Details(id, employeeId);
        }

        //
        // POST: /Tasks/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, int taskTypeId, int taskStatusId, int employeeId, Task task)
        {
            try
            {
                task.Id = id;
                task.TaskStatus = new TaskStatus { Id = taskStatusId };
                task.TaskType = new TaskType { Id = taskTypeId };
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.StoreTask(task, employeeId);
                client.Close();
                this.SetMessage(InfoStrings.TaskUpdated, MessageType.Info);
                return RedirectToAction("Details", "Schedules", new { employeeId = employeeId, date = task.StartTime.ToShortDateString() });
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskUpdating, MessageType.Error);
            }
            return View(task);
        }

        #endregion

        #region Delete

        //
        // GET: /Tasks/Delete/5

        public ActionResult Delete(int id, int employeeId, int tskCount)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                Task task = client.GetTask(id);
                client.Close();
                ViewData["taskDate"] = task.StartTime;
                ViewData["tskCount"] = tskCount;
                ViewData["employeeId"] = employeeId;
                return View(task);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.TaskNotFound, MessageType.Error);
                return RedirectToAction("Index", "Schedules", new { employeeId = employeeId });
            }

        }

        //
        // POST: /Tasks/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, int employeeId, DateTime taskDate, int tskCount, FormCollection collection)
        {
            try
            {
                TaskServiceClient client = new TaskServiceClient();
                client.AddValidationToken(this.Token);
                client.DeleteTask(id);
                client.Close();
                this.SetMessage(InfoStrings.TaskDeleted, MessageType.Info);
                if (tskCount - 1 > 0)
                {
                    return RedirectToAction("Details", "Schedules", new { employeeId = employeeId, date = taskDate });
                }
                else
                {
                    return RedirectToAction("Index", "Schedules", new { employeeId = employeeId });
                }
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorTaskDeleting, MessageType.Error);
            }
            return RedirectToAction("Index", "Schedules", new { employeeId = employeeId });
        } 
        #endregion
    }
}
